<?php
class Index extends Action {
	private $limit = 15;
	/*
	 * (non-PHPdoc) @see Action::execute()
	 */
	public function execute($params) {
		check_login ( ADMIN_LOGIN_PAGE );
		$data = array ();
		
		$start = rqst ( 'start', 1 ); // 分页		
		$where = Model::where_build ( array ('name as AMT.name LIKE', 'type' ), $data, array ('name' => 'like' ) );
		$time1 = rqst ( 'time1' );
		$time2 = rqst ( 'time2' );
		if (! empty ( $time1 )) {
			$data ['time1'] = $time1;
			$where ['create_time >='] = strtotime ( $time1 . ' 00:00:00' );
		}
		if (! empty ( $time2 )) {
			$data ['time2'] = $time2;
			$where ['create_time <='] = strtotime ( $time1 . ' 23:59:59' );
		}
		
		$attModel = new WebAttachmentModel ();
		$attModel->alias ( 'AMT' );
		$attModel->join ( 'core_user AS U', "AMT.create_uid  = U.uid" );
		
		$attModel->sort ( array ('create_time', 'd' ) );
		
		$items = $attModel->where ( $where )->limit ( $this->limit, $start - 1 )->count ( true )->retrieve ( "AMT.*,U.name AS author" );
		
		$data ['limit'] = $this->limit;
		
		$data ['curl'] = unkeepargs ( $this->url, 'start' );
		
		if ($items) {
			$data ['items'] = $items;
			$data ['countTotal'] = $items->countTotal;
		}
		
		$data ['type_options'] = array_merge ( array ('' => '-请选择-' ), UploadTmpFile::getAttachmentTypes () );
		bind ( 'attach_icon', array ($this, 'attach_icon' ), 10, 2 );
		bind ( 'get_attach_actions', array ($this, 'get_attach_actions' ), 10, 2 );
		bind ( 'get_attachment_bench_options', array ($this, 'get_attachment_bench_options' ) );
		return new SmartyView ( $data, 'share/Index' );
	}
	
	public function attach_icon($icon, $item) {
		if ($item ['type'] == 'image') {
			$src = the_thumbnail_src($item ['url'],80,60);			
			return $src;
		} else if ($item ['type'] == 'doc') {
			if ($item ['ext'] == 'xls' || $item ['ext'] == 'xlsx') {
				return 'images/spreadsheet.png';
			} else if ($item ['ext'] == 'txt') {
				return 'images/text.png';
			} else {
				return 'images/document.png';
			}
		} else if ($item ['type'] == 'zip') {
			return 'images/archive.png';
		} else if ($item ['type'] == 'media') {
			return 'images/video.png';
		}
		return 'images/default.png';
	}
	public function get_attach_actions($actions, $item) {
		$actions .= '<a title="编辑" class="edit-attach"  href="./?Ctlr=EditAttach&aid=' . $item ['attachment_id'] . '"><i class="icon-edit"></i>编辑</a>';
		$actions .= '<a title="删除" onclick="return confirm(\'确定要删除该文件?\');" href="./?Ctlr=DelAttach&aid=' . $item ['attachment_id'] . '"><i class="icon-trash"></i>删除</a>';
		if($item['type'] == 'image'){
			$actions .= '<a title="生成缩略图" class="g_thumb" href="./?Ctlr=ThumbPic&aid=' . $item ['attachment_id'] . '"><i class="icon-picture"></i>生成缩略图</a>';
		}
		return $actions;
	}
	public function get_attachment_bench_options($actions) {
		$actions .= '<li><a href="./?Ctlr=ThumbPic&aid=" class="menu-g-thumb"><i class="icon-picture"></i>生成缩略图</a></li>';
		$actions .= '<li><a href="./?Ctlr=DelAttach&aid=" class="menu-del-attach"><i class="icon-trash"></i>永久删除</a></li>';
		return $actions;
	}
}